package com.projectx.action.contact;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.projectx.R;
import com.projectx.util.ActivityManage;

import java.util.ArrayList;
import java.util.List;

public class FindPerson extends AppCompatActivity {
    private Button search;
    private ImageView delete;
    private EditText editText;
    private RecyclerView recyclerView;
    private TextView textView;
    private List<String> data = new ArrayList<>();
    private List<Bitmap> bitmaps = new ArrayList<>();
    private int count=1;
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManage.addActivity("FindPerson", this);
        setContentView(R.layout.activity_findperson);
        search = findViewById(R.id.back);
        delete = findViewById(R.id.delete);
        editText = findViewById(R.id.edittext);
        recyclerView = findViewById(R.id.recycler_view);
        textView = findViewById(R.id.textView);
        data.add("测试1");
        try {
            getSupportActionBar().hide(); //去除标题栏
        } catch (NullPointerException e) {
            Log.d("KMH", "没有状态栏标题");
        }
        editText.setText(FindFriend.text);
        int position = getIntent().getIntExtra("position", 2);
        if (position == 0)
            textView.setText("人");
        if (position == 1)
            textView.setText("群");
        editText.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }//文本改变之前执行

            @Override
            //文本改变的时候执行
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //如果长度为0
                if (s.length() == 0) {
                    //隐藏“删除”图片
                    delete.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.GONE);
                } else {//长度不为0
                    //显示“删除图片”
                    delete.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.VISIBLE);
                }
            }

            public void afterTextChanged(Editable s) {
            }//文本改变之后执行
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.setText("");
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        final FindPersonAdapter findPersonAdapter;
        if (textView.getText().toString().equals("人")){
            findPersonAdapter=new FindPersonAdapter(this, data, bitmaps,false);
        }
        else {
            findPersonAdapter=new FindPersonAdapter(this, data, bitmaps,true);
        }
        recyclerView.setAdapter(findPersonAdapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;
                data.add("测试"+count);
                findPersonAdapter.notifyDataSetChanged();
            }
        });
    }
}

class FindPersonAdapter extends RecyclerView.Adapter<FindPersonAdapter.VH> {

    private Context context;
    private List<String> data;
    private List<Bitmap> bitmaps;
    private boolean isGroup;

    public FindPersonAdapter(Context context, List<String> data, List<Bitmap> bitmaps,boolean isGroup) {
        this.context = context;
        this.data = data;
        this.bitmaps = bitmaps;
        this.isGroup=isGroup;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final VH holder, int position) {
        holder.textView.setText(data.get(position));
        try {
            holder.imageView.setImageBitmap(bitmaps.get(position));
            if(bitmaps.get(position).equals(null)){
                holder.imageView.setImageResource(R.drawable.ic_account_circle_black_36dp);
            }
        }catch (NullPointerException e){
            holder.imageView.setImageResource(R.drawable.ic_account_circle_black_36dp);
        }catch (IndexOutOfBoundsException e){
            Log.e("lzw","头像list越界了");
        }
        holder.click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击事件
                AlertDialog.Builder alert = new AlertDialog.Builder(context);
                if (isGroup==false){
                    alert.setMessage("确认要添加其为好友?");
                }else {
                    alert.setMessage("确认要加入该群?");
                }
                alert.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                                        int which) {
                        if (isGroup==false){
                            TabFragment.data_person.add(holder.textView.getText().toString());
                            TabFragment.bitmaps.add(null);
                           // ContactListFragment contactListFragment=new ContactListFragment();
                            //ContactListFragment.fragments.set(0,new TabFragment(contactListFragment.getContext(),"好友"));
                           // ContactListFragment.tabs.add("好友");
                        }
                        else {
                            TabFragment.data_group.add(holder.textView.getText().toString());
                            TabFragment.bitmaps.add(null);
                           // ContactListFragment contactListFragment=new ContactListFragment();
                            //ContactListFragment.fragments.set(1,new TabFragment(contactListFragment.getContext(),"群聊"));
                            //ContactListFragment.tabs.add("群聊");
                        }
                        Toast.makeText(context, "添加成功", Toast.LENGTH_SHORT).show();
                    }
                });
                alert.setNegativeButton("取消", null);
                alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        dialog.dismiss();
                    }
                });
                alert.show();
            }
        });
    }
    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class VH extends RecyclerView.ViewHolder {

        TextView textView;
        ImageView imageView;
        View click;

        public VH(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
            imageView = itemView.findViewById(R.id.imageView);
            click = itemView.findViewById(R.id.click);
        }
    }
}

